package com.qqtech.qquan.homemaking.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.ad.enums.AdTypeDictEnum;
import com.qqtech.qquan.ad.model.AdBaseVo;
import com.qqtech.qquan.ad.service.AdBaseService;
import com.qqtech.qquan.homemaking.enums.HomemakingBaseConst;
import com.qqtech.qquan.homemaking.enums.HomemakingPubitemStatusDictEnum;
import com.qqtech.qquan.homemaking.enums.HomemakingTypeStatusDictEnum;
import com.qqtech.qquan.homemaking.model.HomemakingPubitemVo;
import com.qqtech.qquan.homemaking.model.HomemakingSysDictQuery;
import com.qqtech.qquan.homemaking.model.HomemakingSysDictVo;
import com.qqtech.qquan.homemaking.model.HomemakingType;
import com.qqtech.qquan.homemaking.model.HomemakingTypeQuery;
import com.qqtech.qquan.homemaking.model.HomemakingTypeVo;
import com.qqtech.qquan.homemaking.service.HomemakingPubitemService;
import com.qqtech.qquan.homemaking.service.HomemakingSysDictService;
import com.qqtech.qquan.homemaking.service.HomemakingTypeService;
import com.qqtech.qquan.user.model.UserAddress;
import com.qqtech.qquan.user.model.UserLoginVo;
import com.qqtech.qquan.user.service.UserAddressService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/homemaking/type")
public class HomemakingTypeController extends
	BaseWebController<HomemakingType, HomemakingTypeQuery> {
	@Resource
	private HomemakingTypeService homemakingTypeService;
	
	@Resource
	private HomemakingSysDictService homemakingSysDictService;
	
	@Resource
	private HomemakingPubitemService homemakingPubitemService;
	
	@Resource
	private UserAddressService userAddressService;
	@Resource
	private AdBaseService adBaseService;

	@Override
	protected BaseService<HomemakingType> getBaseService() {
		return homemakingTypeService;
	}

	/**
	 * 获取一级类别
	 * 
	 * @return
	 */
	@RequestMapping(value = "/findParent")
	@ResponseBody
	public Result findParent() {
		Result result = new Result();
		List<HomemakingTypeVo> list = homemakingTypeService
				.findParent(HomemakingTypeStatusDictEnum.VALID.getCode());
		result.put("parentList", list);
		return result;
	}

	/**
	 * 获取二级类别
	 * 
	 * @param parentId
	 *            一级id
	 * @return
	 */
	@RequestMapping(value = "/findChildren")
	@ResponseBody
	public Result findChildren(@RequestBody HomemakingTypeQuery homemakingTypeQuery) {
		Result result = new Result();
		HomemakingTypeQuery q = new HomemakingTypeQuery();
		q.setParentId(homemakingTypeQuery.getParentId());
		/*List<HomemakingTypeVo> list = homemakingTypeService.findChildren(
				q.getParentId(), HomemakingTypeStatusDictEnum.VALID.getCode());*/
		List<HomemakingTypeVo> list = homemakingTypeService.findChildrenByCache(q.getParentId());
		result.put("childrenList", list);
		HomemakingTypeVo parent = homemakingTypeService.getById(homemakingTypeQuery.getParentId());
		result.put("parentName", parent==null?"":parent.getName());
		//详情页面的服务标准和服务流程
		HomemakingSysDictQuery dictQuery = new HomemakingSysDictQuery(); 
		String code = "type" + homemakingTypeQuery.getParentId().toString();
		dictQuery.setCode(code);
		List<HomemakingSysDictVo> dictList = homemakingSysDictService.queryList(dictQuery);
		if (dictList != null && !dictList.isEmpty()) {
			List<Map<String,Object>> tempList = new ArrayList<Map<String,Object>>();
			for (HomemakingSysDictVo dict : dictList) {
				Map<String,Object> tempMap = new HashMap<String, Object>();
				tempList.add(tempMap);
				if (dict.getImgList() == null) { //没有图片的
					tempMap.put("dictTitle", dict.getTypeDesc());
					tempMap.put("dictContent", dict.getName());
					tempMap.put("hasImg", "0");
				} else {
					List<Map<String,Object>> tempContentList = new ArrayList<Map<String,Object>>();
					for (int i = 0; i < dict.getImgList().size(); i++) {
						Map<String,Object> tempContentMap = new HashMap<String, Object>();
						tempContentList.add(tempContentMap);
						tempContentMap.put("dictContent", dict.getNameList().get(i));
						tempContentMap.put("dictContentImg",dict.getFileDomain() + dict.getImgList().get(i));
					}
					tempMap.put("dictTitle", dict.getTypeDesc());
					tempMap.put("dictContents", tempContentList);
					tempMap.put("hasImg", "1");
				}
			}
			result.put("dictList", tempList);
		}
		return result;
	}
	
	/**
	 * 服务首页
	 * @param homemakingTypeQuery
	 * @return
	 */
	@RequestMapping(value = "/indexType")
	@ResponseBody
	public Result indexType(@RequestBody Map<String, String> map) {
		Result result = new Result();
		Integer communityId = null;
		if (StringUtil.isNotBlank(map.get("communityId"))) {
			communityId = Integer.valueOf(map.get("communityId"));
		}
		// 父级的
		/* 2016年10月13日 改版后不用了
		List<HomemakingTypeVo> list = homemakingTypeService
				.findParent(HomemakingTypeStatusDictEnum.VALID.getCode());
		if (list != null) {
			list = list.subList(0, 3);
			for (HomemakingTypeVo parent : list) {
				List<HomemakingTypeVo> childrenList = homemakingTypeService.findChildren(
						parent.getId(), HomemakingTypeStatusDictEnum.VALID.getCode());
				parent.setChildrenList(childrenList.subList(0, 3));
			}
		}
		result.put("typeList", list);*/
		//广告
		List<AdBaseVo> adList = adBaseService.getAdListByType(AdTypeDictEnum.DISCOVER,communityId);
		Integer rcbjTypeId = null;
		String rcbjTypeName = "";
		String rcbjTypeImg = "";
		String rcbjTypePrice = "";
		String rcbjTypeUnit = "";
		String rcbjTypeRemark = "";
		
		Integer jdwxTypeId = null;
		String jdwxTypeName = "";
		String jdwxTypeImg = "";
		String jdwxTypeRemark = "";
		
		Integer jdqxTypeId = null;
		String jdqxTypeName = "";
		String jdqxTypeImg = "";
		String jdqxTypeRemark = "";
		HomemakingTypeVo rcbjType = homemakingTypeService.getById(HomemakingBaseConst.RI_CHANG_BAO_JIE_ID);
		if (rcbjType != null) {
			List<HomemakingPubitemVo> pubitemVoList = homemakingPubitemService.findByTypeId(
					rcbjType.getId(), HomemakingPubitemStatusDictEnum.VALID.getCode());
			rcbjTypeId = rcbjType.getId();
			rcbjTypeName = rcbjType.getName();
			rcbjTypeImg = rcbjType.getFileDomain()+rcbjType.getImg();
			rcbjTypeRemark = rcbjType.getRemark();
			if (pubitemVoList != null && !pubitemVoList.isEmpty()) {
				rcbjTypePrice = pubitemVoList.get(0).getPrice()==null?"":pubitemVoList.get(0).getPrice().toString();
				rcbjTypeUnit = pubitemVoList.get(0).getUnit();
			}
		}
		HomemakingTypeVo jdwxType = homemakingTypeService.getById(HomemakingBaseConst.JIA_DIAN_WEI_XIU_ID);
		if (jdwxType != null) {
			jdwxTypeId = jdwxType.getId();
			jdwxTypeName = jdwxType.getName();
			jdwxTypeImg = jdwxType.getFileDomain()+jdwxType.getImg();
			jdwxTypeRemark = jdwxType.getRemark();
		}
		HomemakingTypeVo jdqxType = homemakingTypeService.getById(HomemakingBaseConst.JIA_DIAN_QING_XI_ID);
		if (jdqxType != null) {
			jdqxTypeId = jdqxType.getId();
			jdqxTypeName = jdqxType.getName();
			jdqxTypeImg = jdqxType.getFileDomain()+jdqxType.getImg();
			jdqxTypeRemark = jdqxType.getRemark();
		}
		result.put("rcbjTypeId", rcbjTypeId);//日常保洁id
		result.put("rcbjTypeName", rcbjTypeName);//日常保洁
		result.put("rcbjTypeImg", rcbjTypeImg);//日常保洁图片
		result.put("rcbjTypePrice", rcbjTypePrice);//日常保洁价格
		result.put("rcbjTypeUnit", rcbjTypeUnit);//日常保洁单位
		result.put("rcbjTypeRemark", rcbjTypeRemark);
		result.put("jdwxTypeId", jdwxTypeId);//家电维修id
		result.put("jdwxTypeName", jdwxTypeName);//家电维修
		result.put("jdwxTypeImg", jdwxTypeImg);//家电维修图片
		result.put("jdwxTypeRemark", jdwxTypeRemark);
		result.put("jdqxTypeId", jdqxTypeId);//家电清洗id
		result.put("jdqxTypeName", jdqxTypeName);//家电清洗
		result.put("jdqxTypeImg", jdqxTypeImg);//家电清洗图片
		result.put("jdqxTypeRemark", jdqxTypeRemark);
		result.put("adList", adList);
		return result;
	}
	
	
	
	/**
	 * 服务介绍
	 * @param homemakingTypeQuery
	 * @return
	 */
	@RequestMapping(value = "/detailType")
	@ResponseBody
	public Result detailType(@RequestBody HomemakingTypeQuery homemakingTypeQuery) {
		Result result = new Result();
		HomemakingTypeVo type = homemakingTypeService.getById(homemakingTypeQuery.getId());
		//服务的项目
		List<HomemakingPubitemVo> pubitemVoList = homemakingPubitemService.findByTypeId(
				homemakingTypeQuery.getId(), HomemakingPubitemStatusDictEnum.VALID.getCode());
		result.put("pubitemVoList", pubitemVoList);
		//详情页面的服务标准和服务流程
		HomemakingSysDictQuery dictQuery = new HomemakingSysDictQuery(); 
		String code = "type" + homemakingTypeQuery.getId().toString();
		dictQuery.setCode(code);
		List<HomemakingSysDictVo> dictList = homemakingSysDictService.queryList(dictQuery);
		if (dictList != null && !dictList.isEmpty()) {
			List<Map<String,Object>> tempList = new ArrayList<Map<String,Object>>();
			for (HomemakingSysDictVo dict : dictList) {
				Map<String,Object> tempMap = new HashMap<String, Object>();
				tempList.add(tempMap);
				if (dict.getImgList() == null) { //没有图片的
					tempMap.put("dictTitle", dict.getTypeDesc());
					tempMap.put("dictContent", dict.getName());
					tempMap.put("hasImg", "0");
				} else {
					List<Map<String,Object>> tempContentList = new ArrayList<Map<String,Object>>();
					for (int i = 0; i < dict.getImgList().size(); i++) {
						Map<String,Object> tempContentMap = new HashMap<String, Object>();
						tempContentList.add(tempContentMap);
						tempContentMap.put("dictContent", dict.getNameList().get(i));
						tempContentMap.put("dictContentImg",dict.getFileDomain() + dict.getImgList().get(i));
					}
					tempMap.put("dictTitle", dict.getTypeDesc());
					tempMap.put("dictContents", tempContentList);
					tempMap.put("hasImg", "1");
				}
			}
			result.put("dictList", tempList);
		}
		// 默认地址
		UserAddress userAddressQuery = new UserAddress();
		UserLoginVo userLoginVo = super.getLoginUser();
		if (userLoginVo != null) {
			Integer userId = userLoginVo.getId();
			userAddressQuery.setIsDefault(1);
			userAddressQuery.setCreatorId(userId);
			userAddressQuery.setStatus(1);
			Pageable pageKit = new PageKit(1,1, new PageOrder(Direction.DESC, "id"));
			List<UserAddress> userAddressList =  userAddressService.queryList(userAddressQuery, pageKit);
			if(userAddressList != null && userAddressList.size() > 0){
				result.put("userAddress", userAddressList.get(0));
			}
		}
		result.put("type", type);
		return result;
	}
}
